package com.qf.openapi.web.master.filter;

import com.spy.openapi.web.master.utils.Constants;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/logout")
@Slf4j
public class LoginOutFilter implements Filter {
    public LoginOutFilter() {
        super();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("Filter被初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        request.getSession().setAttribute(Constants.SISSION_USER_KEY,null);
        log.debug("删除session中的user对象");
        Cookie cookie = new Cookie(Constants.USER_TOKEN,null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        response.sendRedirect("/ssm2202/login.html");
        log.debug("删除cookie");

    }

    @Override
    public void destroy() {
        log.debug("Filter被销毁");
    }
}
